package com.ht.api.call.func.impl.wwx;

import cn.hutool.json.JSONObject;
import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;

/**
 * 社群消息查询后置函数
 */
public class WwxGroupChatMsgQueryFunctionCall implements IFunctionCall {

    @Override
    public String getName() {
        return "社群消息查询后置函数";
    }

    @Override
    public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
        DbList list = result.getRecords();

        for (Entity msg : list) {
            int fromUsertype = 2;
            String from = msg.getStr("from");
            Entity user = Db.createSqlParser("wx_workuser").select("alias aliasname,avatar,gender,name").where("userid", from).findOne(me.getDataSource());

            if (user == null) {
                String sql = "select oneid,nickname,aliasname,avatar,gender from user_main where status = 0 and bitmap_contains(users, "+Math.abs(from.hashCode())+")";
                user = Db.findOne(me.getDataSource(), sql);
                fromUsertype = 1;
            }
            msg.set("from_user_type", fromUsertype);
            msg.set("mapper_from", user);
        }

        return null;
    }
}
